package view.book;

import java.awt.*;
import java.awt.event.*;


import javax.swing.*;
import javax.swing.border.EmptyBorder;

import entity.Book;
import entity.BookType;
import service.book.BookServer;
import service.book.BookServerImpl;
import utils.ComonUtil;
import view.MainView;
import view.CommonView.CommonJPanel;
import view.model.BookTypeComboBoxModel;

/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 添加图书模块
 *
 */
public class AddBookView extends CommonJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BookTypeComboBoxModel bcm = new BookTypeComboBoxModel();
	private JComboBox<BookType> bookTypeComboBox;
	private JTextField bookIdField, bookNameField,authorField,priceField,bookCountField,bookPressField,bookPressTimeField;
	private JTextArea bookDescField;

	// 创建按钮
	private JButton add;
	private JButton reset;

	BookServer bookServer = new BookServerImpl();
	public AddBookView() {
		super(TYPE_TRANSPARENT);
		setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
		setBorder(new EmptyBorder(20, 100, 10, 100));

		// 标题面板
		JPanel titlePanel = new CommonJPanel(TYPE_TRANSPARENT);
		add(titlePanel);
		// 内容标题
		JLabel labelAddBook = new JLabel("图书添加");
		labelAddBook.setIcon(new ImageIcon(AddBookView.class.getResource("/img/ADD.png")));
		labelAddBook.setFont(new Font("微软雅黑", Font.BOLD, 20));
		titlePanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 70));
		titlePanel.add(labelAddBook);

		// 标签、输入框面板
		JPanel mainPanel = new CommonJPanel(TYPE_TRANSPARENT);
		mainPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
		mainPanel.setPreferredSize(new Dimension(MainView.instance.getWidth(), 280));
		add(mainPanel);

		// 创建图书编号标签
		JLabel labelBookId = new JLabel("图书编号：");
		labelBookId.setIcon(new ImageIcon(AddBookView.class.getResource("/img/book-id-icon.png")));
		mainPanel.add(labelBookId);
		// 创建图书编号输入框
		bookIdField = new JTextField();
		bookIdField.setPreferredSize(new Dimension(260, 30));
		bookIdField.setEditable(false);
		mainPanel.add(bookIdField);

		// 创建图书名称标签
		JLabel labelBookName = new JLabel("图书名称：");
		labelBookName.setIcon(new ImageIcon(AddBookView.class.getResource("/img/book-name-icon.png")));
		mainPanel.add(labelBookName);
		// 创建图书编号输入框
		bookNameField = new JTextField();
		bookNameField.setPreferredSize(new Dimension(260, 30));
		mainPanel.add(bookNameField);

		// 创建图书作者标签
		JLabel labelAuthor = new JLabel("图书作者：");
		labelAuthor.setIcon(new ImageIcon(AddBookView.class.getResource("/img/book-author-icon.png")));
		mainPanel.add(labelAuthor);
		// 创建图书作者输入框
		authorField = new JTextField();
		authorField.setPreferredSize(new Dimension(260, 30));
		mainPanel.add(authorField);

		// 创建图书价格标签
		JLabel labelPrice = new JLabel("图书价格：");
		labelPrice.setIcon(new ImageIcon(AddBookView.class.getResource("/img/book-price-icon.png")));
		mainPanel.add(labelPrice);
		// 创建图书价格输入框
		priceField = new JTextField();
		priceField.setPreferredSize(new Dimension(260, 30));
		mainPanel.add(priceField);

		// 创建图书类别标签
		JLabel labelBookType = new JLabel("图书类别：");
		labelBookType.setIcon(new ImageIcon(AddBookView.class.getResource("/img/bookTypeAddmenu-icon.png")));
		mainPanel.add(labelBookType);
		// 创建图书类别下拉框
		bookTypeComboBox = new JComboBox<>(bcm);
		bookTypeComboBox.setPreferredSize(new Dimension(260, 30));
		bookTypeComboBox.setBackground(Color.white);
		mainPanel.add(bookTypeComboBox);


		// 创建图书库存标签
		JLabel labelBookCount = new JLabel("图书库存：");
		labelBookCount.setIcon(new ImageIcon(AddBookView.class.getResource("/img/kc.png")));
		mainPanel.add(labelBookCount);
		// 创建图书库存输入框
		bookCountField = new JTextField();
		bookCountField.setPreferredSize(new Dimension(260, 30));
		mainPanel.add(bookCountField);
		
		
		//出版
		JLabel labelBookPress = new JLabel("出版社   ：");
		labelBookPress.setIcon(new ImageIcon(AddBookView.class.getResource("/img/cb.png")));
		mainPanel.add(labelBookPress);
		bookPressField = new JTextField();
		bookPressField.setPreferredSize(new Dimension(260, 30));
		mainPanel.add(bookPressField);
		JLabel labelBookPressTime = new JLabel("出版时间：");
		labelBookPressTime.setIcon(new ImageIcon(AddBookView.class.getResource("/img/book-borrow-time-icon.png")));
		mainPanel.add(labelBookPressTime);
		bookPressTimeField = new JTextField();
		bookPressTimeField.setPreferredSize(new Dimension(260, 30));
		mainPanel.add(bookPressTimeField);
		
		

		// 创建图书描述标签
		JLabel labelBookDesc = new JLabel("图书描述：");
		labelBookDesc.setIcon(new ImageIcon(AddBookView.class.getResource("/img/bookTypeDesc-icon.png")));
		mainPanel.add(labelBookDesc);
		// 创建图书描述输入框
		bookDescField = new JTextArea();
		bookDescField.setPreferredSize(new Dimension(680, 80));
		bookDescField.setLineWrap(true);
		bookDescField.setBackground(new Color(246, 246, 246));
		mainPanel.add(bookDescField);

		// 按钮面板
		JPanel buttonPanel = new CommonJPanel(TYPE_TRANSPARENT);
		buttonPanel.setLayout(new GridLayout(1, 2, 100, 0));
		add(buttonPanel);

		// 创建添加按钮
		add = new JButton("添加");
		buttonPanel.add(add);

		// 创建重置按钮
		reset = new JButton("重置");
		buttonPanel.add(reset);

		// 添加按钮事件
		addListener();

		// 重置按钮事件
		reset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				resetInput();
			}
		});

	}

	/**
	 * 添加按钮事件
	 */
	public void addListener() {
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (ComonUtil.isEmpty(bookNameField.getText()) ||ComonUtil.isEmpty(authorField.getText())
						|| ComonUtil.isEmpty(priceField.getText()) 
						|| null == bookTypeComboBox.getSelectedItem()
						|| ComonUtil.isEmpty(bookCountField.getText()) 
						|| ComonUtil.isEmpty(bookDescField.getText())
						|| ComonUtil.isEmpty(bookPressField.getText())
						|| ComonUtil.isEmpty(bookPressTimeField.getText())) {
					JOptionPane.showMessageDialog(AddBookView.this, "选项不能为空！");
					return;
				}
			
				
			
				
				Book book = new Book();		
				if(!ComonUtil.isEmpty(bookIdField.getText())) {
					book.setId(Integer.parseInt(bookIdField.getText()));
				}
				book.setBookAuthor(authorField.getText());
				book.setBookDesc(bookDescField.getText());
				book.setBookName(bookNameField.getText());
				book.setBookPress(bookPressField.getText());
				book.setBookPressTime(bookPressTimeField.getText());
				book.setBookPrice(priceField.getText());
				book.setBorrowingNumbers(Integer.parseInt(bookCountField.getText()));
				book.setBookType(bookTypeComboBox.getSelectedItem().toString());
				
				String flag = bookServer.addBook(book)?"添加成功！":"添加失败";
				JOptionPane.showMessageDialog(AddBookView.this, flag);
			}
		});
	}

	/**
	 * 重置输入框内容
	 */
	public void resetInput() {
		bookIdField.setText("");
		bookNameField.setText("");
		authorField.setText("");
		priceField.setText("");
		bookCountField.setText("");
		bookDescField.setText("");
		bookPressField.setText("");
		bookPressTimeField.setText("");
		bookTypeComboBox.setSelectedIndex(-1);
		bookTypeComboBox.updateUI();
	}



}
